package com.chb.authentication;

import com.chb.entity.SimpleUserDetails;
import com.chb.entity.SecurityUserInfo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Arrays;
import java.util.List;

/**
 * @description: 权限校验
 * @author: CHB
 * @create: 2021-11-17 10:57
 **/

public class SimpleUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SimpleUserDetails userDetails = null;
        // 模拟查询数据库
        switch (username) {
            case "admin": {
                SecurityUserInfo securityUserInfo = new SecurityUserInfo();
                securityUserInfo.setUserId(1L);
                securityUserInfo.setName("admin");
                securityUserInfo.setPwd(new BCryptPasswordEncoder().encode("123"));
                securityUserInfo.setSex(1);
                securityUserInfo.setPhone("17845572997");
                securityUserInfo.setPermissionList(List.of("admin"));
                userDetails = new SimpleUserDetails(securityUserInfo);
                break;
            }
            case "man": {
                SecurityUserInfo securityUserInfo = new SecurityUserInfo();
                securityUserInfo.setUserId(2L);
                securityUserInfo.setName("man");
                securityUserInfo.setPwd(new BCryptPasswordEncoder().encode("456"));
                securityUserInfo.setSex(1);
                securityUserInfo.setPhone("15845362997");
                securityUserInfo.setPermissionList(Arrays.asList("man", "lion"));
                userDetails = new SimpleUserDetails(securityUserInfo);
                break;
            }
            case "woman": {
                SecurityUserInfo securityUserInfo = new SecurityUserInfo();
                securityUserInfo.setUserId(3L);
                securityUserInfo.setName("woman");
                securityUserInfo.setPwd(new BCryptPasswordEncoder().encode("789"));
                securityUserInfo.setSex(0);
                securityUserInfo.setPhone("19845372997");
                securityUserInfo.setPermissionList(List.of("woman"));
                userDetails = new SimpleUserDetails(securityUserInfo);
                break;
            }
            default:
                break;
        }
        return userDetails;
    }
}
